package com.wolan.dao.impl;

import com.wolan.entity.User;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.wolan.dao.IUserDao;
import redis.clients.jedis.JedisCluster;


/**
 * Dao
 * @author http://blog.csdn.net/java2000_wl
 * @version <b>1.0</b>
 */
@Repository
public class UserDao implements IUserDao {

    /*@Autowired
    private StringRedisTemplate stringRedisTemplate;*/

    @Autowired
    private JedisCluster jedisCluster;

    /**
     * 获取 RedisSerializer
     * <br>------------------------------<br>
     */
    /*protected RedisSerializer<String> getRedisSerializer() {
        return stringRedisTemplate.getStringSerializer();
    }*/

    /**
     * 新增
     *<br>------------------------------<br>
     * @param user
     * @return
     */
    public boolean add(final User user) {
        /*boolean result = stringRedisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key  = serializer.serialize(user.getId());
                byte[] name = serializer.serialize(user.getName());
                return connection.setNX(key, name);
            }
        });
        return result;*/
        /*List<String> list = new ArrayList<String>();
        list.add(0,"list1");
        list.add(1,"list2");
        stringRedisTemplate.opsForList().leftPush("list", list.toString());*/
        //stringRedisTemplate.opsForValue().set("hello22", "world22333");
        //stringRedisTemplate.delete("hello22");
        return true;
    }

    /**
     * 通过key获取
     * <br>------------------------------<br>
     * @param keyId
     * @return
     */
    public User get(final String keyId) {
        /*User result = stringRedisTemplate.execute(new RedisCallback<User>() {
            public User doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key = serializer.serialize(keyId);
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                String name = serializer.deserialize(value);
                return new User(keyId, name, null);
            }
        });
        return result;*/
        //System.out.println(stringRedisTemplate.opsForValue().get(keyId));
        System.out.println(jedisCluster.get(keyId));

        //System.out.println(stringRedisTemplate.opsForList().leftPop(keyId));
        return null;
    }

    /**
     * 批量新增 使用pipeline方式
     *<br>------------------------------<br>
     *@param list
     *@return
     */
    /*public boolean add(final List<User> list) {
        Assert.notEmpty(list);
        boolean result = stringRedisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                for (User user : list) {
                    byte[] key  = serializer.serialize(user.getId());
                    byte[] name = serializer.serialize(user.getName());
                    connection.setNX(key, name);
                }
                return true;
            }
        }, false, true);
        return result;
    }

    *//**
     * 删除
     * <br>------------------------------<br>
     * @param key
     *//*
    public void delete(String key) {
        List<String> list = new ArrayList<String>();
        list.add(key);
        delete(list);
    }

    *//**
     * 删除多个
     * <br>------------------------------<br>
     * @param keys
     *//*
    public void delete(List<String> keys) {
        stringRedisTemplate.delete(keys);
    }

    *//**
     * 修改
     * <br>------------------------------<br>
     * @param user
     * @return
     *//*
    public boolean update(final User user) {
        String key = user.getId();
        if (get(key) == null) {
            throw new NullPointerException("数据行不存在, key = " + key);
        }
        boolean result = stringRedisTemplate.execute(new RedisCallback<Boolean>() {
            public Boolean doInRedis(RedisConnection connection)
                    throws DataAccessException {
                RedisSerializer<String> serializer = getRedisSerializer();
                byte[] key  = serializer.serialize(user.getId());
                byte[] name = serializer.serialize(user.getName());
                connection.set(key, name);
                return true;
            }
        });
        return result;
    }
*/

}